package com.xueke.crawler.service.initTree;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xueke.crawler.helper.HttpHelper;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by weibo on 2017/5/13.
 *
 * @author weibo.qin
 * @version 2017/5/13
 */
@Component("ywInitTree")
public class YwInitTree extends BaseInitTree {



    // 语文学科的域名
    static String domain = "http://yw.zxxk.com";
    // 语文学科树形结构文档的api
    static String childNodeUrl = domain
        + "/webapi/childnodes?datatype=${datatype}&dataid=${dataid}&channelid=${channelid}";

    @Override
    public void start() {

        Document doc = HttpHelper.getSimpleDocument(domain + "/jc.html");
        Element tree = doc.getElementById("tree");
        Element channel = tree.select("li:eq(0)").first();

        String channelId = parseChannelId(channel);
        String title = parseTitle(channel);
        String uri = parseUri(channel);
        String dataType = parseDataType(channel);
        int isLeaf = parseIsLeaf(channel);
        System.out.println(channelId + "-----" + title + "--------" + uri
            + "------" + isLeaf);
        saveSubjectTree(channelId, channelId, domain, uri, title, channelId,
            isLeaf, dataType);

        Elements lis = channel.getElementsByTag("ul").select("li");

        for (Element li : lis) {
            String dataId = parseChannelId(li);
            title = parseTitle(li);
            uri = parseUri(li);
            isLeaf = parseIsLeaf(li);
            dataType = parseDataType(li);
            System.out.println(channelId + "-----" + title + "--------" + uri
                + "------" + isLeaf);
            saveSubjectTree(channelId, dataId, domain, uri, title, channelId,
                isLeaf, dataType);
            if (isLeaf == 0) {
                crawleTree(channelId, dataId, dataType);
            }
        }
    }

    private void crawleTree(String channelId, String dataId, String dataType) {
        String url = getChildNodeUrl(channelId, dataId, dataType);
        String json = getJson(url);
        System.out.println(url);
        JSONArray array = (JSONArray) JSONArray.parse(json);

        for (int i = 0; i < array.size(); i++) {
            JSONObject o = array.getJSONObject(i);
            String _dataType = o.getJSONObject("NodeId").getString("DataType");
            String _dataId = o.getJSONObject("NodeId").getString("DataId");
            String _pid = o.getJSONObject("ParentNodeId").getString("DataId");
            String _title = o.getString("NodeName");
            String _uri = o.getString("NodeUrl");
            int _isLeaf = o.getBoolean("IsOpen") ? 1 : 0;

            System.out.println("_dataType=" + _dataType + ",_dataId=" + _dataId
                + ",_pid=" + _pid + ",_title=" + _title + ",_uri=" + _uri
                + ",_isLeaf=" + _isLeaf);

            saveSubjectTree(channelId, _dataId, domain, _uri, _title,
                _pid, _isLeaf, _dataType);
            if (_isLeaf == 0) {
                crawleTree(channelId, _dataId, _dataType);
            }
        }

    }



    private String getJson(String url) {
        try {
            Connection.Response resp = Jsoup.connect(url).execute();
            return resp.body();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String getChildNodeUrl(String channelId, String dataId,
                                   String dataType) {
        return childNodeUrl.replace("${dataid}", dataId)
            .replace("${channelid}", channelId)
            .replace("${datatype}", dataType);
    }

    private String parseUri(Element tree) {
        return tree.select("a").attr("href");
    }

    private String parseTitle(Element tree) {
        return tree.select("a").attr("title");
    }

    private String parseChannelId(Element tree) {

        return tree.select("i").attr("dataid");
    }

    private String parseDataType(Element tree) {

        return tree.select("i").attr("datatype");
    }

    private int parseIsLeaf(Element li) {
        return li.select("i").attr("class").equals("tree-open") ? 1 : 0;
    }
}
